package the.workshop.bu.dto.req;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import the.broccoli.auth.UserContext;
import the.broccoli.setting.entity.BroFile;
import the.workshop.bu.constant.BuConsts;
import the.workshop.bu.dto.CustomerInfoDTO;
import the.workshop.bu.entity.Customer;
import the.workshop.bu.entity.CustomerLegal;
import the.workshop.bu.entity.CustomerTax;

import java.util.Date;
import java.util.List;

@Data
public class CustomerInfoModifyReq {

    /**
     * 客户公司ID
     */
    private Integer customerId;
    private String name;
    /**
     * 公司地址
     */
    private String address;

    /**
     * 营业执照号码
     */
    private String certNumber;

    /**
     * 营业执照发送日期
     */
    private Date certDate;

    /**
     * 开户银行
     */
    private String bankName;

    /**
     * 对公账号
     */
    private String bankNumber;

    /**
     * 纳税等级
     */
    private String taxLevel;

    /**
     * 税务登录密码
     */
    private String taxPassword;

    /**
     * 对公流水年
     */
    private String billWater;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 所属银行（4大行）
     */
    private String bank4Name;

    /**
     * 4大行常用卡号
     */
    private String bank4Number;

    /**
     * 联系电话
     */
    private String tels;



    /**
     * 配偶姓名
     */
    private String spouseName;

    /**
     * 配偶电话号码
     */
    private String spouseTel;

    /**
     * 配偶身份证号码
     */
    private String spouseIdNumber;

    /**
     * 配偶是否知晓
     */
    private Integer spouseIsKnow;

    /**
     * 是否有房产证
     */
    private Integer houseIsCert;

    /**
     * 房产支付方式。全款、贷款
     */
    private String housePayment;

    /**
     * 房产地址
     */
    private String houseAddress;

    /**
     * 房产价格
     */
    private String housePrice;

    /**
     * 房产性质
     */
    private String houseNature;

    /**
     * 房产抵押银行
     */
    private String houseMortgageBank;

    /**
     * 房产抵押到期日期
     */
    private Date houseMortgageDueDate;

    /**
     * 车辆支付方式。全款、贷款
     */
    private String carPayment;

    /**
     * 车辆品牌
     */
    private String carBrand;

    /**
     * 车辆行驶里程
     */
    private String carMileage;

    /**
     * 车辆年限
     */
    private Integer carAgeLimit;

    /**
     * 车辆价格
     */
    private String carPrice;

    /**
     * 工资
     */
    private String salary;

    /**
     * 工资连续月份
     */
    private String salaryMonthCount;

    /**
     * 是否有社保
     */
    private Integer isSocialInsurance;

    /**
     * 是否有商业保险
     */
    private Integer isBusiInsurance;

    /**
     * 是否有公积金
     */
    private Integer isFund;

    /**
     * 保险公司1
     */
    private Integer insuranceCompany1;

    /**
     * 保险费用1
     */
    private Integer insuranceFee1;

    /**
     * 保险公司2
     */
    private Integer insuranceCompany2;

    /**
     * 保险费用2
     */
    private Integer insuranceFee2;

    private String contact1Name;
    private String contact1Tel;
    private String contact1Relation;

    private String contact2Name;
    private String contact2Tel;
    private String contact2Relation;

    /**
     * 纳税情况
     */
    private String taxes;

    /**
     * 客户附件
     */
    private String files;


    public CustomerInfoDTO toCustomerInfoModifyDTO() {
        CustomerInfoDTO dto = new CustomerInfoDTO();

        Customer customer = new Customer();
        BeanUtils.copyProperties(this, customer);
        customer.setId(customerId);
        dto.setCustomer(customer);
        customer.setModifiedTime(new Date());
        customer.setModifiedId(UserContext.userId());
        customer.setModifiedName(UserContext.userName());

        CustomerLegal customerLegal = new CustomerLegal();
        BeanUtils.copyProperties(this, customerLegal);
        customerLegal.setModifiedTime(new Date());
        customerLegal.setModifiedId(UserContext.userId());
        customerLegal.setModifiedName(UserContext.userName());
        dto.setCustomerLegal(customerLegal);

        if (StringUtils.hasText(taxes)) {
            List<CustomerTax> customerTaxList = JSON.parseArray(taxes, CustomerTax.class);
            for (CustomerTax customerTax : customerTaxList) {
                customerTax.setCustomerId(customerId);
                customerTax.setCreatedTime(new Date());
                customerTax.setCreatedId(UserContext.userId());
                customerTax.setCreatedName(UserContext.userName());
                customerTax.setModifiedTime(new Date());
                customerTax.setModifiedId(UserContext.userId());
                customerTax.setModifiedName(UserContext.userName());
            }
           dto.setTaxes(customerTaxList);
        }

        if (StringUtils.hasText(files)) {
            List<BroFile> customerFileList = JSON.parseArray(files, BroFile.class);
            for (BroFile file : customerFileList) {
                file.setTargetId(customerId.toString());
                file.setFileCategory(BuConsts.CUSTOMER_FILE_CATEGORY);
                file.setCreatedTime(new Date());
                file.setCreatedId(UserContext.userId());
                file.setCreatedName(UserContext.userName());
                file.setModifiedTime(new Date());
                file.setModifiedId(UserContext.userId());
                file.setModifiedName(UserContext.userName());
            }
            dto.setFiles(customerFileList);
        }

        return dto;
    }
}
